<template>
  <div class="list-container">
    <!-- 头部面包屑部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="margin-top">
      <!-- 搜索框和添加商品按钮 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="gotoAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格数据 -->
      <el-table :data="goodsList" border class="margin-top" stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格"
          prop="goods_price"
          width="90"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="80"
        ></el-table-column>
        <el-table-column label="添加时间" width="160">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGood(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        class="margin-top"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
// 导入getGoodsListAPI接口
import { getGoodsListAPI } from '@/api/getGoodsListAPI.js'
// 导入deleteGoodAPI接口
import { deleteGoodAPI } from '@/api/deleteGoodAPI.js'
export default {
  name: 'List',
  data() {
    return {
      // 与请求一起发送的查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goodsList: [],
      // 商品信息总数量
      total: 0
    }
  },
  methods: {
    // 获取商品列表信息
    async getGoodsList() {
      const { data: res } = await getGoodsListAPI(this.queryInfo)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      console.log(res)
      this.$message.success('获取商品列表成功')
      this.goodsList = res.data.goods
      this.total = res.data.total
      // console.log(this.goodsList)
      // console.log(this.total)
    },
    // 监听每页条数的变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听页数的变化
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 点击删除按钮删除对应商品
    async removeGood(goodId) {
      const result = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') {
        return this.$message.info('删除已取消')
      }
      const { data: res } = await deleteGoodAPI(goodId)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getGoodsList()
    },
    // 点击添加商品按钮跳转到添加商品页面
    gotoAdd() {
      this.$router.push('/home/goods/add')
    }
  },
  // 当data，methods，props创建好可以用的时候，最早可以拿到数据的时候，也是最好可以发送ajax请求的时候
  created() {
    this.getGoodsList()
  }
}
</script>

<style lang="less" scoped>
.margin-top {
  margin-top: 15px;
}
</style>
